﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.LMS.Domain;
using ZDSoft.LMS.Core;
using ZDSoft.LMS.Service;
using ZDSoft.LMS.Web.Apps;
using System.Text;

namespace ZDSoft.LMS.Web.Controllers
{
    public class RoleController : UcController
    {
        public ActionResult Index(int pageIndex = 1)
        {
            int count = 0;
            IList<Role> list = Container.Instance.Resolve<IRoleService>().GetPaged(null, null, pageIndex, PagerHelper.PageSize, out count);
            PageList<Role> pageList = list.ToPageList<Role>(pageIndex, PagerHelper.PageSize, count);//转换为PageList集合，用于分页控件显示不同的页码
            return View(pageList);
        }

        public ViewResult Authorize()
        {
            string temp = HttpUtility.HtmlEncode(GenerateXml());
            ViewBag.TreeXml = temp;
            return View();
        }

        [HttpPost]
        public ActionResult Authorize(string account,string password)
        {
            GenerateXml();
            return View();
        }

        public ViewResult Create()
        {
            Role role = new Role();
            return View(role);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="role">传会来的角色</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(Role role)
        {
            if (ModelState.IsValid)//如果user实体的数据合法
            {
                //ModelState.AddModelError("RoleName", "角色名称发生了重复哦");

                Container.Instance.Resolve<IRoleService>().Create(role);//创建user实体
                return RedirectToAction("Index");//跳转到Index页面
            }
            
            return View();//停留在原页面
        }

        private string GenerateXml()
        {
            StringBuilder xml = new StringBuilder();
            IList<SystemFunction> functions = GetAll();
            xml.Append("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><tree id=\"0\"><item text=\"Books\" id=\"books\" open=\"1\" im0=\"tombs.gif\" im1=\"tombs.gif\" im2=\"iconSafe.gif\" call=\"1\" select=\"1\"></item></tree>");
            return xml.ToString();
        }

        private IList<SystemFunction> GetAll()
        {
            return Container.Instance.Resolve<ISystemSettingService>().GetAll();
        }


        public ActionResult Details(int id)
        {
            Role role = Container.Instance.Resolve<IRoleService>().Get(id);
            return View(role);
        }

        /* modify by lengyahong 配置角色对应的功能
        public ActionResult AssorFunction(int id,int? topFunctionID)
        {
            //获取页面显示的角色
            Role role = Container.Instance.Resolve<IRoleService>().Get(id);

            //获取下拉框的内容
            IList<SystemFunction> TopFuntions = Container.Instance.Resolve<ISystemFunctionService>().GetAll()
                .Where(m => m.Parent == null)
                .ToList();
            TopFuntions.Insert(0, new SystemFunction() { ID = 0, SystemFunctionName = "-----quanbu-------" });

            ViewBag.TopFuntions = TopFuntions;

            //获取页面中列表的内容
            ViewBag.AllFunctions = Container.Instance.Resolve<ISystemFunctionService>()
                .GetByFunctionID(topFunctionID);

            return View(role);
        }
        */

    }
}
